package com.example.ims.fame;

import com.example.ims.pojo.Supplier;
import com.example.ims.service.SupplierService;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@Component
public class AddSupplierFame extends JDialog {
    private SupplierService supplierService;
    private ApplicationContext context;

    public AddSupplierFame(ApplicationContext context) {
        this.context = context;
        supplierService = context.getBean(SupplierService.class);
        initComponents();
    }

    private void initComponents() {
        setTitle("添加供应商");
        setModal(true);
        setLayout(new BorderLayout());

        // 整体内容面板
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new GridLayout(7, 2));


        // 供应商名称输入框及标签
        JLabel nameLabel = new JLabel("供应商名称:");
        JTextField nameTextField = new JTextField();
        contentPanel.add(nameLabel);
        contentPanel.add(nameTextField);

        // 联系人输入框及标签
        JLabel contactLabel = new JLabel("联系人:");
        JTextField contactTextField = new JTextField();
        contentPanel.add(contactLabel);
        contentPanel.add(contactTextField);

        // 联系电话输入框及标签
        JLabel phoneLabel = new JLabel("联系电话:");
        JTextField phoneTextField = new JTextField();
        contentPanel.add(phoneLabel);
        contentPanel.add(phoneTextField);

        // 基本信息输入框及标签
        JLabel infoLabel = new JLabel("基本信息:");
        JTextField infoTextField = new JTextField();
        contentPanel.add(infoLabel);
        contentPanel.add(infoTextField);

        // 供货历史输入框及标签
        JLabel historyLabel = new JLabel("供货历史:");
        JTextField historyTextField = new JTextField();
        contentPanel.add(historyLabel);
        contentPanel.add(historyTextField);

        // 信用评价输入框及标签
        JLabel creditLabel = new JLabel("信用评价:");
        JTextField creditTextField = new JTextField();
        contentPanel.add(creditLabel);
        contentPanel.add(creditTextField);

        // 确认和取消按钮面板
        JPanel buttonPanel = new JPanel();
        JButton confirmButton = new JButton("确认");
        JButton cancelButton = new JButton("取消");

        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取输入的值
                String name = nameTextField.getText();
                String contact = contactTextField.getText();
                String phone = phoneTextField.getText();
                String info = infoTextField.getText();
                String history = historyTextField.getText();
                String credit = creditTextField.getText();

                try {
                    // 创建Supplier对象并设置属性
                    Supplier supplier = new Supplier();
                    supplier.setName(name);
                    supplier.setContact(contact);
                    supplier.setPhone(phone);
                    supplier.setInfo(info);
                    supplier.setHistory(history);
                    supplier.setCredit(credit);

                    // 调用添加供应商业务逻辑方法
                    supplierService.addSupplier(supplier);

                    // 弹出提示框告知用户添加成功
                    JOptionPane.showMessageDialog(AddSupplierFame.this, "供应商添加成功", "添加提示", JOptionPane.INFORMATION_MESSAGE);
                    dispose(); // 关闭对话框
                } catch (NumberFormatException ex) {
                    // 如果ID等需要数字格式的输入有误，弹出错误提示框
                    JOptionPane.showMessageDialog(AddSupplierFame.this, "输入的数字格式不正确，请检查", "错误", JOptionPane.ERROR_MESSAGE);
                } catch (Exception ex) {
                    // 其他异常，弹出相应错误提示框
                    JOptionPane.showMessageDialog(AddSupplierFame.this, "供应商添加失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose(); // 关闭对话框
            }
        });

        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);

        add(contentPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        // 设置对话框大小并使其显示在相对合适的位置（相对父窗口）
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
    }
}